package com.edu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.entity.Conditions;
import com.edu.entity.Goods;
import com.edu.entity.GoodsToWeb;
import com.edu.entity.Tool;
import com.edu.service.impl.GoodServerImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
@WebServlet(name = "indexFindGoods", urlPatterns = { "/indexFindGoods" })
public class findGoodServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("进入goods");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		GoodServerImpl gsi = new GoodServerImpl();
		Tool t1 = new Tool();
		Goods good = new Goods();
		Conditions con = null;
		con = new Conditions();
		con.setPageNum(1);
		con.setPageSize(6);
		ObjectMapper om = new ObjectMapper();
		String re = request.getParameter("tag");
		if(re == null) {
			re = "数码电子";
		}
		System.out.println(re);
		int tagNum = t1.typeStringToInt(re);
		good.setGoodtype(tagNum);
		ArrayList<GoodsToWeb> l1 = gsi.indexFindGoods(good, con);
		ArrayList<GoodsToWeb> l2 = new ArrayList<GoodsToWeb>();
		for (GoodsToWeb gtw : l1) {
			System.out.println(gtw.toString());
			l2.add(gtw);
		}
		String json = om.writeValueAsString(l2);
		PrintWriter pw = response.getWriter();
		pw.print(json);
		pw.flush();
		return;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
